package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.R;
import com.sgb.kjwl.utils.GlideEngine;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SelfTeamAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context mContext;
    /**
     * 选择图片后返回的LocalMedia
     **/
    private List<LocalMedia> mData;
    /**
     * 本地文件List
     **/
    private List<File> fileList;
    /**
     * 网络图片和本地图片地址组合在一起的List
     **/
    private List<String> urls;
    /**
     * 网络图片地址List
     **/
    private List<String> neImgList;
    private Intent intent;
    /**
     * 上传图片的最大张数
     **/
    private int maxSize = 9;
    /**
     * 脚布局
     **/
    private int TYPE_FOOT = 1001;

    private GridLayoutManager mGridLayoutManager;

    public SelfTeamAlbumAdapter(Context context, GridLayoutManager manager){
        mContext = context;
        this.urls = new ArrayList<>();
        fileList = new ArrayList<>();
        neImgList = new ArrayList<>();
        mLayoutInflater = LayoutInflater.from(context);
        this.mGridLayoutManager = manager;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOT) {
            View headerView = mLayoutInflater.inflate(R.layout.item_website_photo_plus3, parent, false);
            return new FootViewHolder(headerView);
        }
        View inflate = mLayoutInflater.inflate(R.layout.item_self_team_album, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            //如果地址包含http，就用网络加载图片的方式
            RequestOptions options = new RequestOptions();
            options.override(400, 400);
            Glide.with(mContext).load(urls.get(position-1)).apply(options).into(((ViewHolder) holder).img);
            ((ViewHolder) holder).img.setOnClickListener(view -> openPhotoView(position-1));
        } else if (holder instanceof FootViewHolder) {
            ((FootViewHolder) holder).add_rl.setOnClickListener(v -> {
                PictureSelector.create((Activity) mContext)
                        .openGallery(PictureMimeType.ofImage())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .theme(R.style.picture_default_style)
                        .maxSelectNum(maxSize)//判断还能选多少张图片
                        .selectionMedia(mData)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            });
        }
        ViewGroup.LayoutParams parm = holder.itemView.getLayoutParams();
        parm.height =
                mGridLayoutManager.getWidth() / mGridLayoutManager.getSpanCount()
                        - 2 * holder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) parm).leftMargin;
    }

    @Override
    public int getItemCount() {
        //加了头布局,多一个
        if (urls.size() != 0) {
            return urls.size() + 1;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_FOOT;
        }
        return super.getItemViewType(position);
    }

    //设置本地图片
    public void setDatas(List<LocalMedia> mData, List<String> urls) {
        //防止图片重复  可优化
        this.urls.clear();
        this.urls.removeAll(urls);
        if (neImgList.size() != 0) {
            this.urls.addAll(neImgList);
        }
        this.urls.addAll(urls);
        this.mData = mData;
        notifyDataSetChanged();
    }

    /**
     * 设置图片文件List
     **/
    public void setFileList(List<File> fileList) {
        this.fileList = fileList;
    }

    /**
     * 设置网络图片List
     */
    public void setNetImgList(List<String> netImgList) {
        if (netImgList == null) {
            this.neImgList = new ArrayList<>();
        } else {
            this.neImgList = netImgList;
            this.urls.addAll(netImgList);
        }
        notifyDataSetChanged();
    }

    /**
     * 获取已选择的本地文件List
     **/
    public List<File> getFileList() {
        return this.fileList;
    }

    /**
     * 获取所有的地址List，包括网络和本地
     **/
    public List<String> getUrls() {
        return urls;
    }

    /**
     * 获取网络图片地址List
     **/
    public List<String> getNetUrls() {
        return neImgList;
    }

    /**
     * 获取已选择的LocalMedia List
     **/
    public List<LocalMedia> getLocalMedia() {
        return mData;
    }

    /**
     * 预览
     **/
    private void openPhotoView(int position) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setClass(mContext, PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
        mContext.startActivity(intent);
    }

    /**
     * 尾布局的viewholder
     */
    class FootViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout add_rl;

        FootViewHolder(View itemView) {
            super(itemView);
            add_rl = itemView.findViewById(R.id.add_rl);
        }
    }

    /**
     * 正常内容的viewholder
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView delView;

        ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.photo_img);
            delView = itemView.findViewById(R.id.select_iv);
        }
    }
}
